package com.secondshop.mappers;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.secondshop.models.Good;

public interface GoodMapper {
	//根据上传时间排序取第limit个
	@Select("Select * from good_table where status_id = 1 ORDER BY upload_date DESC LIMIT #{0}, #{1};")
	List<Good> getAllGoods(int offset, int limit);
	//根据时间降序查找最新的商品
	@Select("Select * from good_table ORDER BY upload_date DESC;")
	List<Good> getAllGoodList();
	//根据传入的second_type_id 查询最新的且 status_id =1 商品
	@Select("Select * from good_table WHERE second_type_id = #{0} and status_id = 1 ORDER BY upload_date DESC;")
	List<Good> getGoodsByTypeId(Integer secondTypeId);
	//根据传入的second_type_id 查询最新的商品
	@Select("Select * from good_table WHERE second_type_id = #{0} ORDER BY upload_date DESC;")
	List<Good> getGoodsAdminByTypeId(Integer secondTypeId);
	//统计总数
	@Select("Select count(*) from good_table where status_id = 1;")
	int getAllGoodsCount();
	//模糊查询传入 name 查询最新的商品 分页
	@Select("Select * from good_table WHERE name like #{0} and status_id = 1 ORDER BY upload_date DESC LIMIT #{1}, #{2};")
	List<Good> getGoodsBySearch(String searchText, int offset, int limit);
	//模糊查询根据传入的 name 查询统计
	@Select("Select count(*) from good_table WHERE name like #{0} and status_id = 1;")
	int getGoodsBySearchCount(String searchText);

//	@Select("Select * from good_table WHERE second_type_id = #{0} and status_id = 1 ORDER BY upload_date DESC LIMIT #{1}, #{2};")
//	List<Good> getGoodsByType(Integer secondTypeId, int offset, int limit);
	//修改根据第一类别查询
	@Select("Select * from good_table WHERE first_type_id = #{0} and status_id = 1 ORDER BY upload_date DESC LIMIT #{1}, #{2};")
	List<Good> getGoodsByType(Integer firstTypeId, int offset, int limit);
	//根据传入secondTypeId统计商品
	@Select("Select count(*) from good_table WHERE second_type_id = #{0} and status_id = 1;")
	int getGoodsByTypeCount(Integer secondTypeId);
	
	//根据id和star查询商品
	@Select("Select * from good_table WHERE id=#{goodId} and star=#{star}")
	Good getGoodByIdAndStar(int goodId,int star);
	//更新商品推荐
	@Update("UPDATE good_table SET star=#{star} WHERE id=#{goodId}")
	int updateStar(@Param("goodId") int goodId, @Param("star") int star);
	//根据star查询商品
		@Select("Select * from good_table WHERE star=#{star}")
		List<Good> getGoodByStar( int star);
	//根据id 查询商品
	@Select("Select * from good_table WHERE id = #{goodId};")
	Good getGoodById(int goodId);
	
	//根据id 查询商品2
	@Select("Select * from good_table WHERE id = #{goodId};")
	Good getGoodById2(String goodId);
	//根据商品id 和 second_type_id 查询商品列表
	@Select("SELECT * FROM good_table WHERE second_type_id = #{0} AND id != #{1} and status_id = 1 ORDER BY upload_date DESC LIMIT 0, 5;")
	List<Good> getRECGoods(Integer secondTypeId, Integer goodId);
	//根据用户userId查询商品列表
	@Select("SELECT * FROM good_table WHERE user_id = #{userId} and status_id = 1 ORDER BY upload_date DESC;")
	List<Good> getGoodStatusByUserId(Integer userId);
	//根据用户userId查询商品列表
	@Select("SELECT * FROM good_table WHERE user_id = #{userId} ORDER BY upload_date DESC;")
	List<Good> getGoodByUserId(Integer userId);
	//插入商品
	@Insert("insert into good_table (name, photo_url, first_type_id, second_type_id, prise, `describe`, user_id, status_id, upload_date, `update`) "
			+ "values (#{name}, #{photoUrl}, #{firstTypeId}, #{secondTypeId}, #{prise}, #{describe}, #{userId}, 1, now(), now())")
	@Options(useGeneratedKeys = true, keyProperty = "id")
	int insterGood(Good good);
	//更新商品
	@Update("UPDATE good_table SET photo_url = #{0}, `update` = now() WHERE id = #{1};")
	int updateGoodPhotoUrl(String photoUrl, Integer goodId);
	//根据id查询商品的图片路径
	@Select("select photo_url from good_table where id = #{goodId};")
	String getPhotoUrlByGoodId(Integer goodId);
	//更新商品
	@Update("UPDATE good_table SET status_id = #{0}, `update` = now() WHERE id = #{1};")
	int updateGoodStatusId(Integer statusId, Integer goodId);
	//更新商品
	@Update("UPDATE good_table SET name = #{name}, first_type_id = #{firstTypeId}, second_type_id = #{secondTypeId}, prise = #{prise}, `describe` = #{describe} WHERE id = #{id};")
	int updateGood(Good good);
	//根据id删除商品
	@Delete("delete from good_table where id = #{goodId};")
	int deleteGood(Integer goodId);
}
